#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
import typing

from pyoi.constant import DatasetFormat, StorageEngine
from .base_type import BaseType
from pyoi.util import UriUtils
from .base_io import BaseIO


class DataIO(BaseIO):
    def __init__(self, name: str = None, taskName: str = None, uri: str = None,
                 format: typing.Union[DatasetFormat, str] = None, num_partitions: int = None, data=None,
                 schema: 'DataSchema' = None, engine: StorageEngine = None, **kwargs):
        super().__init__(name, taskName, uri, engine)
        self.schema_uri: str = uri + ".schema" + ".json" if uri else None
        self.data = data
        self.schema = schema
        self.format: DatasetFormat = DatasetFormat[format.upper()] if format and isinstance(format, str) else format
        self.num_partitions: int = num_partitions if num_partitions else 4

    def check(self):
        if self.engine and self.uri:
            if self.engine != UriUtils.get_storage_engine_from_uri(uri=self.uri):
                raise RuntimeError(f"{self.engine} not match uri {self.uri}")
        elif self.engine or self.uri:
            self.engine = self.engine or UriUtils.get_storage_engine_from_uri(uri=self.uri)

    @classmethod
    def from_dict(cls, d: typing.Dict[str, object]):
        return DataIO(**d)


class DataSchema(BaseType):
    def __init__(self, include_header: bool = None, delimiter: str = None, columns: list = None, metadata: dict = None):
        self._include_header: bool = include_header
        self._delimiter: str = delimiter
        self._columns = columns if columns else []
        self._metadata = metadata if metadata else {}

    @property
    def include_header(self):
        return self._include_header

    @include_header.setter
    def include_header(self, value: bool):
        self._include_header = value

    @property
    def delimiter(self):
        return self._delimiter

    @delimiter.setter
    def delimiter(self, value: str):
        self._delimiter = value

    @property
    def columns(self):
        return self._columns

    @columns.setter
    def columns(self, value: list):
        self._columns = value

    @property
    def metadata(self):
        return self._metadata

    @metadata.setter
    def metadata(self, value):
        self._metadata = value

    def update_metadata(self, metadata: dict):
        self._metadata.update(metadata)

    @classmethod
    def from_dict(cls, d: dict):
        dataschema = DataSchema(**d)
        return dataschema
